home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung CD 2 (Tewi)(1994).iso / doc / mir / line_num.c < prev    next >
Text File  |  1992-07-02  |  5KB  |  167 lines

  1. /*
  2.  * Usage -  line_num [ starting_line_no ]  < stdin > stdout
  3.  *
  4.  * LINE_NUM Assign a sequence number to each line in a file, starting
  5.  *          either at zero or at a user-specified sequence number.
  6.  *
  7.  * input:   Any printable ASCII file.
  8.  *
  9.  * output:  One line for each line of input.  A sequence number is left
  10.  *          justified, followed by a tab, then the input line exactly as
  11.  *          received.  Empty lines are counted, but left empty.
  12.  *
  13.  *  writeup: MIR TUTORIAL ONE, topic 6
  14.  *
  15.  *  Written:    Douglas Lowry   Feb 17 92
  16.  *  Modified:   Douglas Lowry   May 08 92   argument
  17.  *              Copyright (C) 1992 Marpex Inc.
  18.  *
  19.  *    The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
  20.  *    usage and co-ordination of the MIR family of programs to analyze,
  21.  *    prepare and index databases (small through gigabyte size), and
  22.  *    how to build integrated retrieval software around the MIR search
  23.  *    engine.  The fifth of the five MIR tutorial series explains how
  24.  *    to extend indexing capability into leading edge search-related
  25.  *    technologies.  For more information, GO IBMPRO on CompuServe;
  26.  *    MIR files are in the DBMS library.  The same files are on the
  27.  *    Canada Remote Systems BBS.  A diskette copy of the Introduction
  28.  *    is available by mail ($10 US... check, Visa or Mastercard);
  29.  *    diskettes with Introduction, Tutorial ONE software and the
  30.  *    shareware Tutorial ONE text cost $29.  Shareware registration
  31.  *    for a tutorial is also $29.
  32.  *
  33.  *    E-mail...
  34.  *                Compuserve  71431,1337
  35.  *                Internet    doug.lowry%canrem.com
  36.  *                UUCP        canrem!doug.lowry
  37.  *                Others:     doug.lowry@canrem.uucp
  38.  *
  39.  *    FAX...                  416 963-5677
  40.  *
  41.  *    "Snail mail"...         Douglas Lowry, Ph.D.
  42.  *                            Marpex Inc.
  43.  *                            5334 Yonge Street, #1102
  44.  *                            North York, Ontario
  45.  *                            Canada  M2N 6M2
  46.  *
  47.  *    Related database consultation and preparation services are
  48.  *    available through:
  49.  *              Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
  50.  *              North York, Ontario  Canada   M2J 4Z7
  51.  *              Tel.  416 492-3838   FAX  416 492-3843
  52.  *
  53.  *  This program is free software; you may redistribute it and/or
  54.  *  modify it under the terms of the GNU General Public License as
  55.  *  published by the Free Software Foundation; either version 2 of
  56.  *  the License, or (at your option) any later version.
  57.  *
  58.  *  This program is distributed in the hope that it will be useful,
  59.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  60.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  61.  *  GNU General Public License for more details.
  62.  *
  63.  *  You should have received a copy of the GNU General Public License
  64.  *  (file 05LICENS) along with this program; if not, write to the
  65.  *  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  66.  *  USA.
  67.  */
  68.  
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <ctype.h>
  72.  
  73. #define     MAX_BYTES   4096
  74. #define     repeat      for(;;)
  75.  
  76. /*
  77.  * declarations 
  78.  */
  79.  
  80. typedef     enum        _bool
  81.              { FALSE = 0, TRUE = 1 }  Bool;
  82.  
  83.     void        Usage_(), process();
  84.     char        *Cmdname_() {    return( "line_num" );  }
  85.  
  86. /*
  87.  * MAIN
  88.  */
  89.  
  90. main( argc, argv )
  91.     int  argc;
  92.     char **argv;
  93. {
  94.     long int    bgn_at ;    /*  opening sequence number     */
  95.  
  96.     if( argc > 2 )
  97.         Usage_() ;
  98.     bgn_at = 0 ;
  99.  
  100.     if( argc == 2 )
  101.     {
  102.         if( !isdigit( argv[1][0] ))
  103.             Usage_() ;
  104.         bgn_at = atol( argv[ 1 ] ) ;
  105.     }
  106.  
  107.     process( bgn_at ) ;
  108.  
  109.     exit( 0 );
  110. }
  111. /*
  112.  *  Usage
  113.  */
  114.     void
  115. Usage_()
  116. {
  117.     fprintf( stderr,
  118. "\nUsage:  %s  [ starting_line_no ]  < stdin > stdout\n\n\
  119.         Assign a sequence number to each line in a file, starting\n\
  120.         either at zero or at a user-specified sequence number.\n\n\
  121. input:  Any printable ASCII file.\n\n", Cmdname_() );
  122.     fprintf( stderr,
  123. "output: One line for each line of input.  A sequence number is left\n\
  124.         justified, followed by a tab, then the input line exactly as\n\
  125.         received.  Empty lines are counted, but left empty.\n\n\
  126. writeup: MIR TUTORIAL ONE, topic 6\n\n" ) ;
  127.     exit( 1 ) ;
  128. }
  129. /*
  130.  *  PROCESS
  131.  */
  132.     void
  133. process( bgn_at )
  134.     long int    bgn_at ;
  135. {
  136.     char    line_in[ MAX_BYTES ];
  137.     long int    line_no ;
  138.     int     len ;
  139.  
  140.     line_no = bgn_at - 1 ;
  141.  
  142.     while( fgets( line_in, MAX_BYTES, stdin ) != NULL )
  143.     {
  144.         line_no++ ;
  145.         len = strlen( line_in ) - 1 ;
  146.         if( len > MAX_BYTES - 4 )
  147.         {
  148.             fprintf( stderr, "FATAL... line length exceeds %d bytes.\n",
  149.                 MAX_BYTES ) ;
  150.             exit( 1 ) ;
  151.         }
  152.         line_in[ len ] = '\0' ;
  153.         if( len )
  154.         {
  155.             if( !printf( "%ld\t%s\n", line_no, line_in ))
  156.             {
  157.                 fprintf( stderr, "FATAL... unable to write.\n" );
  158.                 exit( 1 ) ;
  159.             }
  160.         }
  161.         else
  162.             putchar( '\n' );
  163.     }
  164.  
  165.     return ;
  166. }
  167.